﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using H2.NET.Controls;
using H2.NET.Data.Common;
using H2.NET.Business;

namespace H2.NET.Web.Pages.MDC
{
    public partial class TabHkNpl :   TaskControl
    {
        public bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }
        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        public MDCTabStatus Status
        {
            get
            {
                if (StoreAPI["Status~" + Key] == null)
                {
                    StoreAPI["Status~" + Key] = new MDCTabStatus();
                }

                return (MDCTabStatus)StoreAPI["Status~" + Key];
            }
            set
            {
                Key = value.KeyWord;
                StoreAPI["Status~" + Key] = value;
            }
        }
        #region 事件 Page_Load(页面加载)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Status.IsUsed)
            {
                return;
            }
        }
        #endregion
        protected override void OnLoad(EventArgs e)
        {
            InitControls();
            base.OnLoad(e);
        }
        #region 方法 Save(保存数据库)
        public void Save()
        {
            if (!Status.IsEdited)
            {
                return;
            }

            foreach (mdc_HK_NPL_Customer_Application ccc in (List<mdc_HK_NPL_Customer_Application>)gv_HK_NPL.DataSource)
            {
                if (string.IsNullOrEmpty(ccc.Sefi_Code) && string.IsNullOrEmpty(ccc.Cardfile) && string.IsNullOrEmpty(ccc.Cardfile_Q) && string.IsNullOrEmpty(ccc.Cardfile_Q1) && string.IsNullOrEmpty(ccc.Cardfile1) && string.IsNullOrEmpty(ccc.Carton) && string.IsNullOrEmpty(ccc.Carton_Q) && string.IsNullOrEmpty(ccc.Crmo) && string.IsNullOrEmpty(ccc.Crmo_Q) && string.IsNullOrEmpty(ccc.Fxjdai) && string.IsNullOrEmpty(ccc.Fxjdai_Q) && string.IsNullOrEmpty(ccc.Pack_Size) && string.IsNullOrEmpty(ccc.Packager) && string.IsNullOrEmpty(ccc.Packager_Q) && string.IsNullOrEmpty(ccc.Ribbon) && string.IsNullOrEmpty(ccc.Ribbon_Q) && string.IsNullOrEmpty(ccc.SKU_Code) && string.IsNullOrEmpty(ccc.Tagboard) && string.IsNullOrEmpty(ccc.Tagboard_Q) && string.IsNullOrEmpty(ccc.Period))
                {
                    continue;
                }
                if (TaskAPI.Step == "Technical")
                {
                    if (string.IsNullOrEmpty(ccc.Sefi_Code))
                    {
                        continue;
                    }
                }

                ccc.Process = TaskAPI.Process;
                ccc.Incident = TaskAPI.Incident;
                ccc.UserName = UserAPI.UserName;
                ccc.Time = System.DateTime.Now;
                ccc.Status = 1;
                Storage.Save(ccc);
                //string s = ccc.ProductionLine + ccc.Color;
                //TaskAPI["ProductionLine"] = ccc.ProductionLine;
                //TaskAPI["Color"] = ccc.Color;
            }
        }
        #endregion

        #region 方法 InitControls(初始化)
        public void InitControls()
        {
            bool flg = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
            bool flg_plan = TaskAPI.Step=="Planning" && TaskAPI.IsAllowSubmit;
            bool flg_tech = TaskAPI.Step == "Technical" && TaskAPI.IsAllowSubmit;

            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.TextBox, 20, Align.Left, flg, "No", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("01", "SKU", BoundColumnType2.TextBox, 20, Align.Left, flg, "SKU_Code", InputType.None, null));

            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("02", "Plant", BoundColumnType2.TextBox, 60, Align.Left, false, "Plant", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("03", "SEFI Code", BoundColumnType2.TextBox, 70, Align.Left, flg_tech, "Sefi_Code", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("04", "数量1", BoundColumnType2.TextBox, 40, Align.Left, flg_tech, "Pack_Size", InputType.Numeric, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("05", "包装罐", BoundColumnType2.TextBox, 70, Align.Left, flg, "Packager", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("06", "数量2", BoundColumnType2.TextBox, 40, Align.Left, flg, "Packager_Q", InputType.Numeric, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("07", "纸箱", BoundColumnType2.TextBox, 70, Align.Left, flg, "Carton", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("08", "数量3", BoundColumnType2.TextBox, 40, Align.Left, flg, "Carton_Q", InputType.Numeric, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("09", "罐盖", BoundColumnType2.TextBox, 70, Align.Left, flg, "Tagboard", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("10", "数量4", BoundColumnType2.TextBox, 40, Align.Left, flg, "Tagboard_Q", InputType.Numeric, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("11", "标签1", BoundColumnType2.TextBox, 70, Align.Left, flg, "Cardfile", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("12", "数量5", BoundColumnType2.TextBox, 40, Align.Left, flg, "Cardfile_Q", InputType.Numeric, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("13", "标签2", BoundColumnType2.TextBox, 70, Align.Left, flg, "Cardfile1", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("14", "数量6", BoundColumnType2.TextBox, 40, Align.Left, flg, "Cardfile_Q1", InputType.Numeric, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("15", "碳带", BoundColumnType2.TextBox, 70, Align.Left, flg, "Ribbon", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("16", "数量7", BoundColumnType2.TextBox, 40, Align.Left, flg, "Ribbon_Q", InputType.Numeric, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("17", "封箱胶带", BoundColumnType2.TextBox, 70, Align.Left, flg, "Fxjdai", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("18", "数量8", BoundColumnType2.TextBox, 40, Align.Left, flg, "Fxjdai_Q", InputType.Numeric, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("19", "缠绕膜", BoundColumnType2.TextBox, 70, Align.Left, flg, "Crmo", InputType.None, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("20", "数量9", BoundColumnType2.TextBox, 40, Align.Left, flg, "Crmo_Q", InputType.Numeric, null));
            gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("21", "有效期", BoundColumnType2.Calender, 100, Align.Left, flg_plan, "Period", InputType.None, null));
            //gv_HK_NPL.DynamicColumn.Add(new BoundColumn2("21", "备注", BoundColumnType2.TextBox, 70, Align.Left, flg, "Remark", InputType.Numeric, null));
            SetVisible(flg, Add, Delete);

            gv_HK_NPL.Width = 1200;
            if (!IsPostBack)
            {
                this.BOM_P1.Visible = true;
                gv_HK_NPL.Key = Key;
                if (TaskAPI.IsInitiate)
                {
                    List<mdc_HK_NPL_Customer_Application> mdc_HK_NPL = new List<mdc_HK_NPL_Customer_Application>();
                    for (int i = 0; i < 5; i++)
                    {
                        mdc_HK_NPL.Add(new mdc_HK_NPL_Customer_Application());
                        mdc_HK_NPL[i].No = i + 1;
                    }
                    gv_HK_NPL.DataSource = mdc_HK_NPL;
                }
                else
                {
                    IList<mdc_HK_NPL_Customer_Application> mdc_HK_NPL = (mdc_HK_NPL_Customer_Application.process == TaskAPI.Process & mdc_HK_NPL_Customer_Application.incident == TaskAPI.Incident & mdc_HK_NPL_Customer_Application.status == 1).List<mdc_HK_NPL_Customer_Application>();
                    if (mdc_HK_NPL.Count == 0)
                    {
                        for (int i = 0; i < 5; i++)
                        {
                            mdc_HK_NPL.Add(new mdc_HK_NPL_Customer_Application());
                            mdc_HK_NPL[i].No = i + 1;
                        }
                    }
                    gv_HK_NPL.DataSource = mdc_HK_NPL;
                }
            }
        }
        #endregion
        #region 事件 Add(增加行)
        protected void Add_Click(object sender, EventArgs e)
        {
            List<mdc_HK_NPL_Customer_Application> sefi_1_s = (List<mdc_HK_NPL_Customer_Application>)gv_HK_NPL.DataSource;
            mdc_HK_NPL_Customer_Application sefi_1 = new mdc_HK_NPL_Customer_Application();
            //sefi_1.ID = sefi_1_s.Count + 1;
            sefi_1_s.Insert(sefi_1_s.Count, sefi_1);
            gv_HK_NPL.DataSource = sefi_1_s;
            gv_HK_NPL.DataBind();
        }
        #endregion
        #region 事件 Delete(删除行)
        protected void Delete_Click(object sender, EventArgs e)
        {
            List<mdc_HK_NPL_Customer_Application> base_1_s = (List<mdc_HK_NPL_Customer_Application>)this.gv_HK_NPL.DataSource;
            IList<mdc_HK_NPL_Customer_Application> base1 = (mdc_HK_NPL_Customer_Application.process == TaskAPI.Process & mdc_HK_NPL_Customer_Application.incident == TaskAPI.Incident).List<mdc_HK_NPL_Customer_Application>();
            if (base_1_s.Count == 1)
            {

                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('至少保留最后一个 Sheet ！',false)");
                return;

            }
            base_1_s.RemoveAt(gv_HK_NPL.ChangedRowIndex == -1 || gv_HK_NPL.ChangedRowIndex >= base_1_s.Count ? base_1_s.Count - 1 : gv_HK_NPL.ChangedRowIndex);
            bool flag = false;
            foreach (mdc_HK_NPL_Customer_Application base_1 in base1)
            {

                foreach (mdc_HK_NPL_Customer_Application base_2 in base_1_s)
                {
                    if (base_2.ID == base_1.ID)
                    {
                        flag = true;
                        break;
                    }
                }
                if (!flag)
                {
                    Storage.Delete(base_1);
                }
                flag = false;
            }
            gv_HK_NPL.DataSource = base_1_s;
            gv_HK_NPL.DataBind();

        }
        #endregion
        #region 方法 gv_HK_NPL 数据取得
        protected void gv_HK_NPL_DataBinding(object sender, EventArgs e)
        {
            //if (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit && gv_CCC_1.ChangedColumnIndex == 9)

        }
        #endregion

        #region 方法 DataValidation(数据验证)
        public List<string> DataValidation()
        {
            //如果当前 Tabs 是隐藏的情况，退出
            if (!(Status.IsUsed && TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit))
            {
                return new List<string>();
            }

            List<string> vald_s = new List<string>();
            List<Regular> regs = new List<Regular>();
            int i = 1;
            foreach (mdc_HK_NPL_Customer_Application ccc in (List<mdc_HK_NPL_Customer_Application>)this.gv_HK_NPL.DataSource)
            {
                if (string.IsNullOrEmpty(ccc.Sefi_Code) && string.IsNullOrEmpty(ccc.Cardfile) && string.IsNullOrEmpty(ccc.Cardfile_Q) && string.IsNullOrEmpty(ccc.Cardfile_Q1) && string.IsNullOrEmpty(ccc.Cardfile1) && string.IsNullOrEmpty(ccc.Carton) && string.IsNullOrEmpty(ccc.Carton_Q) && string.IsNullOrEmpty(ccc.Crmo) && string.IsNullOrEmpty(ccc.Crmo_Q) && string.IsNullOrEmpty(ccc.Fxjdai)&&string.IsNullOrEmpty(ccc.Fxjdai_Q)&&string.IsNullOrEmpty(ccc.Pack_Size)&&string.IsNullOrEmpty(ccc.Packager)&&string.IsNullOrEmpty(ccc.Packager_Q)&&string.IsNullOrEmpty(ccc.Ribbon)&&string.IsNullOrEmpty(ccc.Ribbon_Q)&&string.IsNullOrEmpty(ccc.SKU_Code)&&string.IsNullOrEmpty(ccc.Tagboard)&&string.IsNullOrEmpty(ccc.Tagboard_Q)&&string.IsNullOrEmpty(ccc.Period))
                {
                    continue;
                }
                if (TaskAPI.Step == "Technical")
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, ccc.Sefi_Code, "&nbsp;&nbsp;行" + i + "Sefi_Code 必填!"));

                }

                if (!string.IsNullOrEmpty(ccc.Pack_Size))
                {
                    regs.Add(new Regular(RegularExp.Digit3, ccc.Pack_Size, "&nbsp;&nbsp;行" + i + "数量1: 不能超过3位小数!"));
                }
                if (!string.IsNullOrEmpty(ccc.Packager_Q))
                {
                    regs.Add(new Regular(RegularExp.Digit3, ccc.Packager_Q, "&nbsp;&nbsp;行" + i + "数量2: 不能超过3位小数!"));
                }
                if (!string.IsNullOrEmpty(ccc.Carton_Q))
                {
                    regs.Add(new Regular(RegularExp.Digit3, ccc.Carton_Q, "FINI BOM &nbsp;&nbsp;行" + i + "数量3: 不能超过3位小数!"));
                }
                if (!string.IsNullOrEmpty(ccc.Tagboard_Q))
                {
                    regs.Add(new Regular(RegularExp.Digit3, ccc.Tagboard_Q, "FINI BOM &nbsp;&nbsp;行" + i + "数量4: 不能超过3位小数!"));
                }
                if (!string.IsNullOrEmpty(ccc.Cardfile_Q))
                {
                    regs.Add(new Regular(RegularExp.Digit3, ccc.Cardfile_Q, "FINI BOM &nbsp;&nbsp;行" + i + "数量5: 不能超过3位小数!"));
                }
                if (!string.IsNullOrEmpty(ccc.Cardfile_Q1))
                {
                    regs.Add(new Regular(RegularExp.Digit3, ccc.Cardfile_Q1, "FINI BOM &nbsp;&nbsp;行" + i + "数量6: 不能超过3位小数!"));
                }
                if (!string.IsNullOrEmpty(ccc.Ribbon_Q))
                {
                    regs.Add(new Regular(RegularExp.Digit3, ccc.Ribbon_Q, "FINI BOM &nbsp;&nbsp;行" + i + "数量7: 不能超过3位小数!"));
                }
                if (!string.IsNullOrEmpty(ccc.Fxjdai_Q))
                {
                    regs.Add(new Regular(RegularExp.Digit3, ccc.Fxjdai_Q, "FINI BOM  &nbsp;&nbsp;行" + i + "数量8: 不能超过3位小数!"));
                }
                if (!string.IsNullOrEmpty(ccc.Crmo_Q))
                {
                    regs.Add(new Regular(RegularExp.Digit3, ccc.Crmo_Q, "FINI BOM &nbsp;&nbsp;行" + i + "数量9: 不能超过3位小数!"));
                }
                  
               
                i++;
            }

            if (i == 1)
            {
                t_Attachment list = (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == TaskAPI.Incident).ListFirst<t_Attachment>();
                if (list == null)
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, list, "&nbsp;&nbsp;" + "FINI BOM 表单不能为空或者必须上传附件!"));
                }

            }
            vald_s.AddRange(Validation(regs));

            if (vald_s.Count > 0)
            {

            }

            return vald_s;
        }
        #endregion
    }
}
